package com.itheima.webSocket;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.entity.AccountMessage;
import jakarta.websocket.Encoder;
import jakarta.websocket.EndpointConfig;

public class AccountMessageJSON implements Encoder.Text<AccountMessage> {
    private static ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String encode(AccountMessage message) {
        try {
            return objectMapper.writeValueAsString(message); // 将 DataJson 对象转换为 JSON 字符串
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void init(EndpointConfig endpointConfig) {
        // 初始化方法（可选）
    }

    @Override
    public void destroy() {
        // 销毁方法（可选）
    }
}
